/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","comm/datatable/datatable","bch/app/classManager/classList.servers","bch/app/classManager/classList.ui"],
    function($,$bb,$datatable,$servers,$ui) {
    var classList = {};

    /**
     * 新增班级成功后的回调函数
     */
    var ajaxAddClassCallback = function(data,className){
        $("." + className).modal("hide");
        $bb.openPage("bch_classDetail",data);
    }

    /**
     * 点击“保存”
     */
    var onSubmit = function(){
        var className = $(this).data("className");
        var channelId = $bb.getChannelId();
        var project = $bb.getProiect();
        var doctorName = JSON.parse($bb.getSessionStorage("account")).alias;
        var queueName = $("#modal_queueName").val();
        var updateTime = $("#modal_updateTime").val();
        var queueIntro = $("#modal_queueIntro").val();
        if( !queueName ){
            $bb.toastShow("请输入班级名称");
        }else if( !updateTime ){
            $bb.toastShow("请输入开班时间");
        }else{
            var params = {
                "oprInfo":{
                    "opCode":"1103",
                    "opType":"2",
                    "opNote":"【"+doctorName+"】医生添加【"+queueName+"】班级",
                    "channelType":"2",
                    "channelId":channelId,
                },
                "queueInfo":{
                    "queueType":"2",
                    "queueUsertype":"0",
                    "defaultOption":"0",
                    "queueName":queueName,
                    "updateTime":updateTime,
                    "queueIntro":queueIntro,
                },
                "queueRoleInfo":{
                    "projectRoleId":project.id,
                    "projectId":project.projectId,
                    "priorityValue":"rw",
                },
                "projectQueueInfo":{
                    "projectId":project.projectId,
                }
            }
            $servers.ajaxAddClass(params,ajaxAddClassCallback,className);
        }
    }

    /**
     * 取消新增班级
     */
    var onCancel = function(){
        var className = $(this).data("className");
        $("." + className).modal("hide");
    }

    /**
     * 点击添加患者
     */
    var onAddClass = function(){
        var className = "modal-addClass";
        $bb.modal({
            "className": className,
            "tit": "添加班级",
        },function(){
            $ui.addClass({
                "dom":$("." + className + " .modal-body"),
            });
            $("." + className).modal("show");
            $("#addClassSubmit").unbind("click");
            $("#addClassSubmit").data("className",className);
            $("#addClassSubmit").click(onSubmit);
            $("#addClassCancel").unbind("click");
            $("#addClassCancel").data("className",className);
            $("#addClassCancel").click(onCancel);
        });
    }

    classList.init = function(){
        // 添加班级
        $("#addClass").unbind("click");
        $("#addClass").click(onAddClass);
        // datatable
        var id = $(".layout-bottom table").attr("id");
        $datatable.init({
            "id": id,
            "options": $servers.getOptions()
        });
    }

    return classList;
});